<?php
class ControllerCatalogCategory extends Controller {
	public function index() {

		$this->load->model('catalog/category');

		$this->load->model('catalog/product');

		$this->load->model('marketing/marketing');

		$this->load->model('tool/image');

		if (isset($this->request->get['filter'])) {
			$filter = $this->request->get['filter'];
		} else {
			$filter = '';
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'p.sort_order';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = $this->config->get('config_product_limit');
		}

		if (isset($this->request->get['category_id'])) {

			$category_id = (int)$this->request->get['category_id'];

		} else {
			$category_id = 0;
		}

		if (isset($this->request->get['filter_viewed'])) {
		    $filter_viewed = (int)$this->request->get['filter_viewed'];

		} else {
		    $filter_viewed = '';
		}

		$category_info = $this->model_catalog_category->getCategory($category_id);

		$data['products'] = array();

		$filter_data = array(
		    'filter_filter'      => $filter,
		    'sort'               => $sort,
		    'order'              => $order,
		    'start'              => ($page - 1) * $limit,
		    'limit'              => $limit,
		    'filter_viewed'      => $filter_viewed
		);





		if ($category_info) {
		    $filter_data['filter_category_id'] = $category_id;
		}
		$product_total = $this->model_catalog_product->getTotalProducts($filter_data);

		$results = $this->model_catalog_product->getProducts($filter_data);


		foreach ($results as $result) {
		    if ($result['image']) {
		        $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
		    } else {
		        $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
		    }

		    $data['products'][] = array(
		        'product_id'  => $result['product_id'],
		        'image'       => $image,
		        'name'        => $result['name'],
		        'price'       => $result['price'],
		        'likes'       => $result['likes'],
		        'points'      => $result['points']
		    );
		}

		$data['total'] = $product_total;
		$data['pageIndex'] = $page;
		$data['pageSize'] = $limit;

		$this->response->setDataResult(1,"",$data);
	}



	public function getOrderProducts(){
	    $this->load->model('catalog/product');

	    $this->load->model('tool/image');


	    if (isset($this->request->get['page'])) {
	        $page = $this->request->get['page'];
	    } else {
	        $page = 1;
	    }

	    if (isset($this->request->get['limit'])) {
	        $limit = $this->request->get['limit'];
	    } else {
	        $limit = $this->config->get('config_product_limit');
	    }

	    $data['products'] = array();

	    $filter_data = array(
	        'start'              => ($page - 1) * $limit,
	        'limit'              => $limit
	    );

	    $results = $this->model_catalog_product->getOrderProducts($filter_data);


	    foreach ($results as $result) {
	        if ($result['image']) {
	            $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
	        } else {
	            $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
	        }

	        $data['products'][] = array(
	            'product_id'  => $result['product_id'],
	            'image'       => $image,
	            'name'        => $result['name'],
	            'firstname'       => $result['firstname']
	        );
	    }

	    $this->response->setDataResult(1,"成功",$data);
	}

	public function personal()
	{

	    $this->load->model('catalog/product');

	    $this->load->model('tool/image');

	    if (isset($this->request->get['page'])) {
	        $page = $this->request->get['page'];
	    } else {
	        $page = 1;
	    }

	    if (isset($this->request->get['limit'])) {
	        $limit = $this->request->get['limit'];
	    } else {
	        $limit = $this->config->get('config_product_limit');
	    }

	    if(isset($this->request->get['customer_id']))
	    {
	        $customer_id = $this->request->get['customer_id'];
	    }
	    else
	    {
	        if($this->customer->isLogged())
	        {
	            $customer_id = $this->customer->getId();
	        }
	        else
	        {
	            $customer_id = 0;
	        }
	    }
	    $self = false;
	    if($this->customer->isLogged())
	    {
	        if($customer_id == $this->customer->getId())
	        {
	            $self = true;
	        }
	    }

	    $filter_data = array(
	        'customer_id'      => $customer_id,
	        'self'      => $self,
	        'start'              => ($page - 1) * $limit,
	        'limit'              => $limit,
	    );

	    $product_total = $this->model_catalog_product->getTotalProductsByCustomer($filter_data);

	    $results = $this->model_catalog_product->getProductsByCustomer($filter_data);

	    $data['products'] = array();

	    foreach ($results as $result) {

	        $data['products'][] = array(
	            'product_id'  => $result['product_id'],
	            'image'       => $result['image'],
	            'name'        => $result['name'],
	            'price'       => $result['price'],
	            'customer_name'           => $result['customer_name'],
	            'customer_image'           => $result['customer_image'],
	            'customer_rank'           => $result['customer_rank'],
	            'address'           => $result['address'],
	            'lat'           => $result['lat'],
	            'lng'           => $result['lng'],
	            'likes'           => $result['likes'],
	            'order_total'           => $result['order_total'],
	            'customer_id'           => $result['customer_id'],
	            'times'         => $result['times']
	        );
	    }


	    $data['total'] = $product_total;
	    $data['pageIndex'] = $page;
	    $data['pageSize'] = $limit;

	    $this->response->setDataResult(1,"",$data);
	}
}